<%@ page contentType="text/html; charset=UTF-8" %>
<%@ include file="/common/common.jsp" %>


<%
    pagemenustr += "addBtn(tb,'生成工资','I','accept',function(){importRecords(this)});";
    pagemenustr += "addBtn(tb,'查看工资','R','accept',function(){viewRecords()});";
%>
<html>
<head>
    <%@ include file="/common/head.jsp" %>
    <script src='${ctx}/dwr/interface/SalaryService.js'></script>
    <script src='${ctx}/dwr/interface/DataService.js'></script>
    <script type="text/javascript">
        var total = 0;
        var currentCount = 0;
        var lostnum = 0;
        var refresstimer;
        var pbar1;
        var isfinish = "0";
        var dlg0;
        Ext.onReady(function() {
            //==== Progress bar1  ====
            pbar1 = new Ext.ProgressBar({
                text:'0%'
            });

            <%if(!pagemenustr.equals("")){%>
            var tb = new Ext.Toolbar();
            tb.render('pagemenubar');
            <%=pagemenustr%>
            <%}%>
            tb.addButton([
                {
                    id:'lockbtn',
                    text : "封帐",
                    iconCls:Ext.ux.iconMgr.getIcon('lock_add'),
                    handler : function() {
                        $.ajax({
                                data:{riqi:document.getElementById('month').value,closed:'1'},
                                async:true,
                                type:'POST',
                                url:'${ctx}/ext/com.cinsea.salary.ext.SalaryAction?action=fengzhang',
                                success:function (data) {
                                    Ext.getCmp('I').disable();
                                    Ext.getCmp('lockbtn').disable();
                                    Ext.getCmp('unlockbtn').enable();
                                },
                                error:function (data) {

                                }
                            });
                    }
                },
                {
                    id:'unlockbtn',
                    text : "解封",
                    iconCls:Ext.ux.iconMgr.getIcon('lock_delete'),
                    handler : function() {
                        $.ajax({
                            data:{riqi:document.getElementById('month').value,closed:'0'},
                            async:true,
                            type:'POST',
                            url:'${ctx}/ext/com.cinsea.salary.ext.SalaryAction?action=fengzhang',
                            success:function (data) {
                                Ext.getCmp('I').enable();
                                Ext.getCmp('lockbtn').enable();
                                Ext.getCmp('unlockbtn').disable();
                            },
                            error:function (data) {

                            }
                        });
                    }
                }
            ]);
            Ext.getCmp('I').disable();
            Ext.getCmp('lockbtn').disable();
            Ext.getCmp('unlockbtn').disable();
            DataService.getValue("select closed from ut_salary where riqi='" + document.getElementById('month').value + "'", {callback:function(v) {
                        if (v=='1') {
                            Ext.getCmp('I').disable();
                            Ext.getCmp('lockbtn').disable();
                            Ext.getCmp('unlockbtn').enable();
                        } else {
                            Ext.getCmp('I').enable();
                            Ext.getCmp('lockbtn').enable();
                            Ext.getCmp('unlockbtn').disable();
                        }
                    }})
            dlg0 = new Ext.Window({
                layout:'border',
                closeAction:'hide',
                plain: true,
                modal :true,
                width:400,
                height:400,
                buttons: [

                    {
                        text     : '<%=I18n.getText("close")%>',
                        handler  : function() {
                            dlg0.hide();

                        }

                    }
                ],
                items:[
                    {
                        id:'dlgpanel',
                        region:'center',
                        contentEl:'dlgdiv',
                        autoScroll:true
                    }
                ]
            });
            dlg0.render(Ext.getBody());
            //Viewport
            var viewport = new Ext.Viewport({
                layout: 'border',
                items: [
                    {region:'center',autoScroll:true,contentEl:'mydiv'}
                ]
            });

        })
        var Runner = function() {
            var f = function(pbar, count, cb) {

                return function() {
                    doRefresh();
                    if (isfinish == "1") {
                        clearInterval(refresstimer);
                        cb();
                    } else {
                        var i = currentCount / count;
                        pbar.updateProgress(i, Math.round(100 * i) + '%');
                    }
                };
            };
            return {
                run : function(pbar, count, cb) {
                    var ms = 5000 / count;
                    try {
                        refresstimer = setInterval(f(pbar, count, cb), 2);
                    } catch(e) {
                    }
                }
            }
        }();
        function checkClosed(){
            DataService.getValue("select closed from ut_salary where riqi='" + document.getElementById('month').value + "'", {callback:function(v) {
                        if (v=='1') {
                            Ext.getCmp('I').disable();
                            Ext.getCmp('lockbtn').disable();
                            Ext.getCmp('unlockbtn').enable();
                        } else {
                            Ext.getCmp('I').enable();
                            Ext.getCmp('lockbtn').enable();
                            Ext.getCmp('unlockbtn').disable();
                        }
                    }})
        }
    </script>
</head>
<body>

<div id="mydiv">
    <div id="pagemenubar" style="z-index:100;"></div>
    <form name="mainForm" id="mainForm" method="post" >
        <input type="hidden" name="dowids" id="dowids">
        <table style="border:0">
            <colgroup>
                <col width="1%">
                <col width="20%">
                <col width="79%">
            </colgroup>

            <TR>
                <TD class=Spacing colspan=3></TD>
            <TR>
            <TR>
                <TD class=Line colspan=3></TD>
            <TR>
            <TR>
                <TD class=Spacing colspan=3 align="center">请选择月份:<input type=text class=inputstyle size=10 id="month" name="month"  value="<%=StringUtil.null2String(DateUtil.getCurrentMonth())%>" onclick="WdatePicker({dateFmt:'yyyy-MM',minDate:'2000-01-01'})" onchange="checkClosed()"></TD>
            <TR>
            <TR>
                <TD class=Line colspan=3></TD>
            <TR>

        </table>
    </form>

</div>
<div id="dlgdiv">
    <div id="progressBarhome">
        <div id="progressBar">
            <div class="status" id="p1text"></div>
            <div id="p1" style="width:300px;display:inline"></div>
            <div id="importMessage">
                生成的记录总数：&nbsp;&nbsp;<span id="totalnum">0</span><br/>
                    <span id="curra">当前正生成第 <span id="curnum">0
                    </span> 条记录</span><span id="finishmessage" style="display:none">操作完成!</span><br/>
            </div>
        </div>
    </div>
</div>
<script language="javascript">
    var btn;

    function importRecords(b) {
        mon = document.getElementById("month").value;
        if (!mon) {
            alert('请选择月份');
            return;
        }
        yes("确定要生成"+mon+"月工资吗？",function(){
        btn = b;
        isfinish = "0";

        btn.disable();
        total = 0;
            currentCount = 0;
            Ext.fly('p1').setDisplayed(true);
            Ext.fly('p1text').update('');
            if (!pbar1.rendered) {
                pbar1.render('p1');
            } else {
                pbar1.text = '0%';
                pbar1.show();
            }
            showprocessbar(mon);
            if (total == -1) {
                pbar1.reset(true);
                btn.enable();
                Ext.fly('p1text').update('请等待先前的任务完成').show();
                return;
            }
            if (total == 0) {
                btn.enable();
                pbar1.reset(true);

                var progressBarhome = document.getElementById("progressBarhome");
                var progressBar = document.getElementById("progressBar");
                progressBarhome.appendChild(progressBar);
                document.getElementById("importMessage").style.display = "none";
                Ext.fly('p1text').update('没有记录').show();
                return;
            }
            Runner.run(pbar1, total, function() {
                pbar1.reset(true);
                //document.getElementById("succesRecord").innerText = "点击查看";
                document.getElementById("finishmessage").innerText = "操作完成!";
                btn.enable();
            });
            }) ;
    }

    function doImportOpt(month) {
            dwr.engine.setAsync(false);
            SalaryService.salary(month, returnTotal);
            dwr.engine.setAsync(true);
    }

    function returnTotal(o) {
        total = o;
        document.getElementById("totalNum").innerText = total;
    }
    function returnCurrentCount(o) {
        var obj =o;

        var curNum = obj;
        document.getElementById("curnum").innerText = curNum;
        currentCount = curNum;
        if (curNum == total) {
            isfinish="1";
            document.getElementById("curra").style.display = "none";
            document.getElementById("finishmessage").style.display = "block";
            document.getElementById("finishmessage").innerText = "操作已完成";

        }
    }
    function doRefresh() {

        SalaryService.getCurrentCount(returnCurrentCount);

    }


    function showprocessbar(month) {
        dlg0.show();
        doImportOpt(month);
    }

    function viewRecords(b) {
        mon = document.getElementById("month").value;
        if (!mon) {
            alert('请选择月份');
            return;
        }
        onUrl('${ctx}/ReportServer?reportlet=/cw/salarylist.cpt&riqi='+mon,mon+'月工资单')
    }
</script>
<script type="text/javascript" language="javascript" src="<%= request.getContextPath()%>/datapicker/WdatePicker.js"></script>
 </body>
</html>
